﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

/// <summary>
/// Contains miscellaneous functionality
/// </summary>
public static class Utilities
{
	static Utilities()
	{
		//
		// TODO: Add constructor logic here
		//
	}

	/// <summary>
	/// Send Email
	/// </summary>
	/// <param name="from">Sender email address</param>
	/// <param name="to">Recipient email address</param>
	/// <param name="subject">Subject of email</param>
	/// <param name="body">Email message</param>
	public static void SendMail(string from, string to, string subject, string body)
	{
		// Configuration mail client
		SmtpClient mailClient = new SmtpClient(ElegantFoodConfiguration.MailServer, ElegantFoodConfiguration.MailServerPort);

		// Enable SSL
		mailClient.EnableSsl = true;

		// Set credentials (for SMTP servers that require authentication)
		mailClient.Credentials = new NetworkCredential(ElegantFoodConfiguration.MailUser, ElegantFoodConfiguration.MailPassword);

		// Create the mail message
		MailMessage message = new MailMessage(from, to, subject, body);

		// is body html format?
		message.IsBodyHtml = true;

		// Send mail
		mailClient.Send(message);
	}

	public static void LogError(Exception error)
	{
		// Get the current date and time
		string dateTime = DateTime.Now.ToLongDateString() + ", at " + DateTime.Now.ToShortDateString();

		// Stores the error message
		string errorMessage = "Exception generated on " + dateTime;

		// Obtain the page that generated error
		System.Web.HttpContext context = System.Web.HttpContext.Current;

		// Build the error message
		errorMessage += "\n\n Page location: " + context.Request.RawUrl;
		errorMessage += "\n\n Message: " + error.Message;
		errorMessage += "\n\n Source: " + error.Source;
		errorMessage += "\n\n Method: " + error.TargetSite;
		errorMessage += "\n\n Stack Trace: \n\n" + error.StackTrace;

		// Send error email in case the option is activated in web.config
		if (ElegantFoodConfiguration.EnableErrorLogMail)
		{
			string from = ElegantFoodConfiguration.MailFrom;
			string to = ElegantFoodConfiguration.ErrorLogMail;
			string subject = "ElegantFood Error Report";
			string body = errorMessage;
			SendMail(from, to, subject, body);
		}
	}

	/// <summary>
	/// Security encrypt string to readable BitConverter
	/// </summary>
	/// <param name="password">String to be encrypted</param>
	/// <returns>Readable BitConverter</returns>
	public static string Encrypted(string str)
	{
		Byte[] OriginalByte;
		Byte[] EncodeByte;
		MD5 md5;

		// Instantiate MD5CrytoServiceProvider, get bytes for original password and compute hash (encoded password)
		md5 = new MD5CryptoServiceProvider();
		OriginalByte = ASCIIEncoding.Default.GetBytes(str);
		EncodeByte = md5.ComputeHash(OriginalByte);

		// Convert encoded byted back to a readable string
		return BitConverter.ToString(EncodeByte);
	}

	​​​​// Get active login user ID
	public static string UserLoginID()
	{
		return Membership.GetUser().ProviderUserKey.ToString();
	}
}